<?php
require_once __DIR__ . '/../../functions.php';

header('Content-Type: application/json');

// 检查管理员权限
if (!isAdmin()) {
    echo json_encode(['success' => false, 'message' => '您没有权限访问此信息']);
    exit;
}

$orderId = intval($_GET['id'] ?? 0);

if ($orderId <= 0) {
    echo json_encode(['success' => false, 'message' => '订单ID无效']);
    exit;
}

$db = getDB();

// 获取订单详情（管理员可以查看所有订单）
$stmt = $db->prepare("SELECT o.*, u.username, u.phone, u.group_name FROM orders o 
                     LEFT JOIN users u ON o.user_id = u.id 
                     WHERE o.id = ?");
$stmt->execute([$orderId]);
$order = $stmt->fetch();

if (!$order) {
    echo json_encode(['success' => false, 'message' => '订单不存在']);
    exit;
}

// 处理图片URL
if ($order['order_screenshot']) {
    $order['screenshot_url'] = UPLOAD_URL . 'orders/' . $order['order_screenshot'];
}

if ($order['settlement_screenshot']) {
    $order['settlement_screenshot_url'] = UPLOAD_URL . 'settlements/' . $order['settlement_screenshot'];
}

// 获取配置选项标签
$order['delivery_location_label'] = getConfigOptionLabel('delivery_location', $order['delivery_location']);
$order['order_platform_label'] = getConfigOptionLabel('order_platform', $order['order_platform']);
$order['payment_method_label'] = getPaymentMethodText($order['payment_method']);

// 添加取件码信息
$order['pickup_code'] = $order['pickup_code'] ?? '';
$order['pickup_code_label'] = !empty($order['pickup_code']) ? $order['pickup_code'] : '无';

echo json_encode(['success' => true, 'data' => $order]);
?>